% REDNOISE SUBROUTINE
% 
% This subroutine calculates a red noise based on an 
% AR1 autocorrelation assuming data with a fixed sampling step d.
% Applying a Monte-Carlo simulation loop, the AR1 is repeated Nsim times,
% Nsim being the value chosen by the user.
%
%
% Inputs : 
% rho = value of the lag-1 autocorrelation coefficient.
% N = length of red noise desired if only a red noise simulation is performed,
% value provided by the user
% OR timex = one column array with time or depth corresponding to each
% values of the signal studied
% Nsim = number of the simulations 
%
% Ouputs :
% redtab = array of nmesure*nsim dimensions, where each column correspon to
% a red noise signal.
%
% Doroth�e Husson, November 2012

function [redtab,nt] = Rednoise(timex,rho,nsim)
nt=length(timex);
% disp('How many values ?');
% nt=input('number of values for the red noise : ');
rzero=0;
redtab=[];
red=[];
i=1;
j=1;
srho=sqrt(1-rho^2);
for i=1:nsim
    white=srho*randn(1);
    red(1)=rho*rzero+white;
    for j=2:nt
        white=srho*randn(1);
        red(j)=rho*red(j-1)+white;
    end
    redtab(:,i)=red;
end
end

